<template>
  <a-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="乡镇" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-dict-select-tag type="list" v-decorator="['fwzId', validatorRules.fwzId]" :trigger-change="true"
                             dictCode="tb_jrtx_fwz,township_name,id" placeholder="请选择乡镇"/>
        </a-form-item>
        <a-form-item label="镇总村数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'villageNum', validatorRules.villageNum]" placeholder="请输入镇村数"
                          style="width: 100%"/>
        </a-form-item>
        <a-form-item label="信用村数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-tooltip>
            <template slot="title">
              没有信用村或未评选时，输入0
            </template>
            <a-input-number v-decorator="[ 'creditVillageNum', validatorRules.creditVillageNum]" placeholder="请输入信用村数"
                            style="width: 100%"/>
          </a-tooltip>

        </a-form-item>
        <a-form-item label="信用村所占百分比" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-tooltip>
            <template slot="title">
              例如:5%
            </template>
            <a-input v-decorator="[ 'creditVillagePersent', validatorRules.creditVillagePersent]"
                     placeholder="请输入信用村所占百分比"></a-input>
          </a-tooltip>
        </a-form-item>
        <a-form-item label="镇总户数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="[ 'householdsNum', validatorRules.householdsNum]" placeholder="请输入镇总户数"
                          style="width: 100%"/>
        </a-form-item>
        <a-form-item label="信用户数" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-tooltip>
            <template slot="title">
              没有信用村或未评选时，输入0
            </template>
            <a-input-number v-decorator="[ 'creditHouseholdsNum', validatorRules.creditHouseholdsNum]"
                            placeholder="请输入信用户数" style="width: 100%"/>
          </a-tooltip>
        </a-form-item>
        <a-form-item label="信用户所占百分比" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-tooltip>
            <template slot="title">
              例如:7%
            </template>
            <a-input v-decorator="[ 'householdsPersent', validatorRules.householdsPersent]"
                     placeholder="请输入信用户所占百分比"></a-input>
          </a-tooltip>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
    import {httpAction} from '@/api/manage'
    import pick from 'lodash.pick'
    import JDictSelectTag from "@/components/dict/JDictSelectTag"
    export default {
        name: "CreditTownshipModal",
        components: {
            JDictSelectTag,
        },
        data() {
            return {
                form: this.$form.createForm(this),
                title: "操作",
                width: 800,
                visible: false,
                model: {},
                labelCol: {
                    xs: {span: 24},
                    sm: {span: 5},
                },
                wrapperCol: {
                    xs: {span: 24},
                    sm: {span: 16},
                },
                confirmLoading: false,
                validatorRules: {
                    fwzId: {rules: [{ required: true, message: '乡镇不能为空'}]},
                    creditVillageNum: {},
                    creditHouseholdsNum: {},
                    householdsNum: {rules: [{ required: true, message: '总户数不能为空'}]},
                    creditVillagePersent: {},
                    villageNum: {rules: [{ required: true, message: '总村数不能为空'}]},
                    householdsPersent: {},
                },
                url: {
                    add: "/creditsystem/creditTownship/add",
                    edit: "/creditsystem/creditTownship/edit",
                }

            }
        },
        created() {
        },
        methods: {
            add() {
                this.edit({});
            },
            edit(record) {
                this.form.resetFields();
                this.model = Object.assign({}, record);
                this.visible = true;
                this.$nextTick(() => {
                    this.form.setFieldsValue(pick(this.model, 'fwzId', 'creditVillageNum', 'creditHouseholdsNum', 'householdsNum', 'creditVillagePersent', 'villageNum', 'householdsPersent'))
                })
            },
            close() {
                this.$emit('close');
                this.visible = false;
            },
            handleOk() {
                const that = this;
                // 触发表单验证
                this.form.validateFields((err, values) => {
                    if (!err) {
                        that.confirmLoading = true;
                        let httpurl = '';
                        let method = '';
                        if (!this.model.id) {
                            httpurl += this.url.add;
                            method = 'post';
                        } else {
                            httpurl += this.url.edit;
                            method = 'put';
                        }
                        let formData = Object.assign(this.model, values);
                        console.log("表单提交数据", formData)
                        httpAction(httpurl, formData, method).then((res) => {
                            if (res.success) {
                                that.$message.success(res.message);
                                that.$emit('ok');
                            } else {
                                that.$message.warning(res.message);
                            }
                        }).finally(() => {
                            that.confirmLoading = false;
                            that.close();
                        })
                    }

                })
            },
            handleCancel() {
                this.close()
            },
            popupCallback(row) {
                this.form.setFieldsValue(pick(row, 'fwzId', 'creditVillageNum', 'creditHouseholdsNum', 'householdsNum', 'creditVillagePersent', 'villageNum', 'householdsPersent'))
            }

        }
    }
</script>